import { Component, OnInit, Input, OnDestroy } from '@angular/core'
import {
  IChargeBack,
  ProcessFlagMap,
  EChargeBackTransCode,
  EChargeBackProcessType,
  ChargeBackTransCodeMap,
  ChargeBackProcessTypeMap,
} from '@shared/interfaces/operation'
import { IBaseInfoColumn } from '@shared/interfaces/baseInfo'
import { DetailBaseClass } from '@shared/classes/DetailBaseClass'
import { I18NService } from '@core'
import { Router, ActivatedRoute } from '@angular/router'
import { PageService } from '@shared/services/page.service'
import { CustomACLService } from '@shared/services/acl.service'
import { ECustomAcl } from '@shared/enums'
import { AmtFormatPipe } from '@shared/filters/amt-format.pipe'

@Component({
  selector: 'app-charge-back-detail-comb',
  templateUrl: './charge-detail.component.html',
})
export class ChargeBackDetailCombComponent extends DetailBaseClass<IChargeBack> implements OnInit, OnDestroy {
  firstColumns: IBaseInfoColumn[]
  secondColumns: IBaseInfoColumn[]
  thirdColumns: IBaseInfoColumn[]

  @Input() detail: IChargeBack

  constructor(
    public i18n: I18NService,
    public router: Router,
    public route: ActivatedRoute,
    public pageService: PageService,
    private customACLService: CustomACLService,
    private amtFormatPipe: AmtFormatPipe,
  ) {
    super(pageService)
  }

  ngOnInit() {
    this.detailData = this.detail
    this.initACLSetting()
    this.initFirstColumns()
    this.initSecondColumns()
    this.initThirdColumns()
    this.initColumns()
  }

  ngOnDestroy() {
    this.customACLService.removeACLAbility(ECustomAcl.ChargeDebitAndRepresent)
  }

  initACLSetting(): void {
    if (this.detailData.processType === EChargeBackProcessType.DebitAndRepresent) {
      this.customACLService.addACLAbility(ECustomAcl.ChargeDebitAndRepresent)
    }
  }

  initColumns(): void {
    this.initValue.call(this, this.firstColumns)
    this.initValue.call(this, this.secondColumns)
    this.initValue.call(this, this.thirdColumns)
  }

  initFirstColumns(): void {
    this.firstColumns = [
      {
        span: 6,
        index: 'fileDate',
        label: '差错日期',
        labelI18n: 'OPERATION.CHARGEBACK.DATE',
      },
      {
        span: 6,
        index: 'transAmt',
        label: '差错金额',
        labelI18n: 'TRADE.CHARGEBACK.AMT',
        asyncValue: this.amtFormatPipe.transform(this.detailData.transAmt, this.detailData.transCurr),
      },
      {
        span: 6,
        index: 'acqReferData',
        label: 'ARN',
      },
      {
        span: 6,
        label: '差错类型',
        labelI18n: 'OPERATION.CHARGEBACK.TRANSCODE',
        displayValue: this.parseTransCode(this.detailData.transCode),
      },
    ]
  }

  initSecondColumns(): void {
    this.secondColumns = [
      {
        span: 6,
        index: 'reasonCode',
        label: '原因代码',
        labelI18n: 'OPERATION.CHARGEBACK.REASONCODE',
      },
      {
        span: 6,
        index: 'processType',
        label: '处理方式',
        labelI18n: 'OPERATION.CHARGEBACK.HANDLEMETHOD',
        displayValue: this.parseProcessType(this.detailData.processType),
      },
      {
        span: 6,
        index: 'operator',
        label: '操作用户',
        labelI18n: 'OPERATION.HOLDRELEASE.DETAIL.OPERATOR',
      },
      {
        span: 6,
        index: 'processFlag',
        label: '状态',
        labelI18n: 'GENERAL.INFO.STATUS',
        pipe: processFlag => this.trans(ProcessFlagMap[processFlag]),
      },
    ]
  }

  initThirdColumns(): void {
    this.thirdColumns = [
      {
        span: 4,
        index: 'downTransAmt',
        label: '扣款金额',
        asyncValue: this.amtFormatPipe.transform(this.detailData.downTransAmt, this.detailData.downTransCurr),
        labelI18n: 'OPERATION.CHARGEBACK.DEBITAMT',
        acl: [ECustomAcl.ChargeDebitAndRepresent],
      },
      {
        span: 4,
        index: 'upTransAmt',
        label: '再请款金额',
        asyncValue: this.amtFormatPipe.transform(this.detailData.upTransAmt, this.detailData.upTransCurr),
        labelI18n: 'OPERATION.CHARGEBACK.REPRESENTAMT',
        acl: [ECustomAcl.ChargeDebitAndRepresent],
      },
      {
        span: 16,
        index: 'remarks',
        label: '备注',
        labelI18n: 'GENERAL.INFO.REMARK',
      },
    ]
  }

  /** 差错类型 */
  parseTransCode(transCode: EChargeBackTransCode): string {
    return this.trans(ChargeBackTransCodeMap[transCode])
  }

  /** 处理方式 */
  parseProcessType(processType: EChargeBackProcessType): string {
    if (processType as string === '0') {
      /** 未处理 */
      return this.trans('OPERATION.CHARGEBACK.PROCESSFLAG.NOTHANDLED')
    }
    return this.trans(ChargeBackProcessTypeMap[processType])
  }

  trans(key: string, params?: Object): string {
    return this.i18n.fanyi(key, params)
  }
}
